//
//  AppDelegate.m
//  KuYouZiXun
//
//  Created by lanou on 15/8/4.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "AppDelegate.h"

#import "KYZXNewsVC.h"

#import "KYZXLeftMeunVC.h"

#import "KYZXScreen.h"

#import "KYZXVideoVC.h"

#import "KYZXStrategyViewController.h"

# import "KYZXSubscriptionVC.h"
#import "KYZXUserInfo.h"
@interface AppDelegate ()


@property (strong, nonatomic)KYZXLeftMeunVC *leftVC;

@property (strong, nonatomic)UIView *tabBarView;

@end

@implementation AppDelegate

- (void)dealloc{

    [[KYZXUserInfo standardKYZXUserInfo] removeObserver:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
    [self setupTabBarTextStyle];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //添加tabbarItem
    KYZXNewsVC *newsVC = [[KYZXNewsVC alloc] init];
    UINavigationController *newsNav= [[UINavigationController alloc] initWithRootViewController:newsVC];
    KYZXVideoVC *videoVC = [[KYZXVideoVC alloc] init];
    UINavigationController *videoNav = [[UINavigationController alloc] initWithRootViewController:videoVC];
    KYZXStrategyViewController *strategyVC = [[KYZXStrategyViewController alloc] init];
    KYZXSubscriptionVC *subscriptionVC = [[KYZXSubscriptionVC alloc] init];
    
     _leftVC = [[KYZXLeftMeunVC alloc] initWithNibName:@"KYZXLeftMeunVC" bundle:nil];
    newsVC.leftMeunVC = _leftVC;
    videoVC.leftMeunVC = _leftVC;
    
    //中间的一层隐藏图层
    UIView *glassView= [[UIView alloc] initWithFrame:self.window.bounds];
    glassView.backgroundColor = [UIColor blackColor];
    glassView.alpha = 0;
    newsVC.glassView = glassView;
    videoVC.glassView = glassView;
    //根视图控制器
    UIViewController *rootVC = [[UIViewController alloc] init];
    UITabBarController *tabBabVC = [[UITabBarController alloc] init];
    [tabBabVC addChildViewController:newsNav];
    [tabBabVC addChildViewController:videoNav];
    [tabBabVC addChildViewController:strategyVC];
    [tabBabVC addChildViewController:subscriptionVC];
    [rootVC.view addSubview:tabBabVC.view];
    [rootVC addChildViewController:tabBabVC];
    [rootVC.view addSubview:glassView];
    [rootVC.view addSubview:_leftVC.view];
    self.window.rootViewController = rootVC;
    [self setupTabBar:tabBabVC.tabBar];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
    NSString *infoStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"配色方案"];
    [self changeColorByInfoStr:infoStr];
    
    [[KYZXUserInfo standardKYZXUserInfo] addObserver:self];
    
    _tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
    _tabBarView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
    [tabBabVC.tabBar insertSubview:_tabBarView atIndex:0];
    tabBabVC.tabBar.opaque = YES;
      return YES;
}

- (void)changeColor:(NSNotification *)info{

    [self changeColorByInfoStr:info.userInfo[@"配色方案"]];
}

- (void)changeColorByInfoStr:(NSString *)infoStr{

    if([infoStr isEqualToString:@"日间模式"] || infoStr == nil){
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
    else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }

}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    _tabBarView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
}



//初始化tabBar
- (void)setupTabBar:(UITabBar *)tabBar
{
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    // 对item设置相应地图片
    item0.selectedImage = [[UIImage imageNamed:@"gs_main_icon_news_highligth_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item0.image = [[UIImage imageNamed:@"gs_main_icon_news_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item0.title = @"新闻";
    item1.selectedImage = [[UIImage imageNamed:@"gs_main_icon_video_highligth_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item1.image = [[UIImage imageNamed:@"gs_main_icon_video_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item1.title = @"视频";
    item2.selectedImage = [[UIImage imageNamed:@"gs_main_icon_strategy_highligth_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item2.image = [[UIImage imageNamed:@"gs_main_icon_strategy_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item2.title = @"攻略";
    item3.selectedImage = [[UIImage imageNamed:@"gs_main_icon_subscribe_highligth_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    item3.image = [[UIImage imageNamed:@"gs_main_icon_subscribe_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item3.title = @"订阅";
}


//初始化tabBar字体样式
- (void)setupTabBarTextStyle{

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0f], NSForegroundColorAttributeName : [UIColor grayColor]
                                                        } forState: UIControlStateNormal];
    // doing this results in an easier to read unselected state then the default iOS 7 one
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:189 / 250.0 green:68 / 250.0 blue:22 / 250.0 alpha:1]
                                                        } forState:UIControlStateSelected];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
